package com.gusha.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.math.BigInteger;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysClassifyDao;
import com.gusha.model.SysClassify;


/**
 * @Created：2018-10-30 14:25:23
 * @Author LightSound的代码生成器
 * @Description:SysClassifyService服务类
 */

@Service
@Transactional
public class SysClassifyService {

    @Autowired
    private SysClassifyDao sysClassifyDao;

    public Page<SysClassify> findAll(Pageable pageable, SysClassify sysClassify) {
        @SuppressWarnings("serial")
        Page<SysClassify> result = sysClassifyDao.findAll(new Specification<SysClassify>() {
            @Override
            public Predicate toPredicate(Root<SysClassify> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysClassify.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysClassify.getId()));
                }
                if (sysClassify.getParentId() != null) {
                    list.add(cb.equal(root.get("parentId").as(BigInteger.class),sysClassify.getParentId()));
                }
                if (StringUtils.isNotBlank(sysClassify.getName())) {
                    list.add(cb.like(root.get("name").as(String.class),"%"+sysClassify.getName()+"%"));
                }
                if (sysClassify.getSort() != null) {
                    list.add(cb.equal(root.get("sort").as(Integer.class),sysClassify.getSort()));
                }
                if (sysClassify.getCreateTime() != null) {
                    list.add(cb.equal(root.get("createTime").as(Date.class),sysClassify.getCreateTime()));
                }
                if (sysClassify.getUpdateTime() != null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),sysClassify.getUpdateTime()));
                }
                if (StringUtils.isNotBlank(sysClassify.getCreator())) {
                    list.add(cb.equal(root.get("creator").as(String.class),sysClassify.getCreator()));
                }
                if (StringUtils.isNotBlank(sysClassify.getUpdator())) {
                    list.add(cb.equal(root.get("updator").as(String.class),sysClassify.getUpdator()));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
